package org.clockworkmages.games.anno1186.tools;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.clockworkmages.games.anno1186.gui.GameUiUtil;

public class ThreadDumpLogger {

	public static void createThreadDumpAfter5sec() {
		final Thread someThread = new Thread(new Runnable() {
			@Override
			public void run() {
				long targetTime = new Date().getTime() + 5000;
				while (new Date().getTime() < targetTime) {
					// do nothing
				}
				// try {
				// wait(5000);
				// } catch (Exception e) {
				// e.printStackTrace();
				// }
				Map<Thread, StackTraceElement[]> traces = Thread
						.getAllStackTraces();
				Iterator<Thread> i = traces.keySet().iterator();
				while (i.hasNext()) {
					Thread thd = i.next();
					System.out.println("*** Thread id" + thd.getId() + ":"
							+ thd.getName() + " ***");
					StackTraceElement[] trace = traces.get(thd);
					for (int j = 0; j < trace.length; ++j) {
						GameUiUtil.logError(trace[j].toString());
						// System.out.println(trace[j]);
					}
				}
			}
		});
		someThread.setDaemon(true);
		someThread.start();
	}

}
